let util = require('../../utils/until.js')

let api = require('../../config/api.js')


Page({
    data: {
        id: 0,
        goods: {},//商品数据
        attribute: [],//商品参数
        issueList: [],//常见问题数据
        comment: [],//评论
        brand: {},//制造商信息
        specificationList: [],//规格列表
        productlist: [],//一个商品对应的不同产品
        checkedSpecPrice: 0,//商品价格
        checkedSpecText: '规格数量选择',//商品基本信息下面的标准文字
        content: '',//详情的富文本
        openAttr: false,//控制规格选择界面是否显示
        tmpSpecText: '请选择规格数量',
        relatedGoods: [],//大家都在看
        number: 1,//商品数量
        openAttr: false,//控制规则选择界面是否显示
        cartGoodsCount: 0,// 购物车的数量
    },
    //判断规格是否选择完整
    isCheckedAllSpec: function () {
        return !this.getCheckedSpecValue().some(function (v) {
            if (v.valueId == 0) {
                return true;
            }
        })
    },
    //获取选中的规格信息
    getCheckedSpecValue: function () {
        let checkedValues = [];
        let _specificationList = this.data.specificationList;
        for (let i = 0; i < _specificationList.length; i++) {
            let _checkedObj = {
                name: _specificationList[i].name,
                valueId: 0,
                valueText: ''
            }
            for (let j = 0; j < _specificationList[i].valueList.length; j++) {
                if (_specificationList[i].valueList[j].checked) {
                    _checkedObj.valueId = _specificationList[i].valueList[j].id;
                    _checkedObj.valueText = _specificationList[i].valueList[j].value;
                }
            }
            checkedValues.push(_checkedObj);
        }
        return checkedValues;
    },
    // 点击不同的规格
    clickSkuValue: function (event) {
        let specName = event.currentTarget.dataset.name;
        let specValueId = event.currentTarget.dataset.valueId;
        let _specificationList = this.data.specificationList;
        for (let i = 0; i < _specificationList.length; i++) {
            if (_specificationList[i].name === specName) {
                for (let j = 0; j < _specificationList[i].valueList.length; j++) {
                    if (_specificationList[i].valueList[j].id == specValueId) {
                        //如果已经选中，则反选
                        if (_specificationList[i].valueList[j].checked) {
                            _specificationList[i].valueList[j].checked = false;
                        } else {
                            _specificationList[i].valueList[j].checked = true;
                        }
                    } else {
                        _specificationList[i].valueList[j].checked = false;
                    }
                }
            }
        }
        this.setData({
            specificationList: _specificationList,
        });
        //重新改变Spec信息
        this.changeSpecInfo();
    },
    //规格变化时，重新计算价格及显示信息
    changeSpecInfo: function () {
        let checkedNameValue = this.getCheckedSpecValue()
        //设置选择的信息
        let checkedValue = checkedNameValue.filter(function (v) {
            if (v.valueId != 0) {
                return true;
            } else {
                return false;
            }
        }).map(function (v) {
            return v.valueText;
        });
        if (checkedValue.length > 0) {
            this.setData({
                tmpSpecText: checkedValue.join(' ')
            })
        } else {
            this.setData({
                tmpSpecText: "请选择规格数量"
            })
        }
        //设置商品基本信息下面的状态
        if (this.isCheckedAllSpec()) {
            this.setData({
                checkedSpecText: this.data.tmpSpecText
            })
        }
    },

    //规格变化时，重新计算价格及显示信息
    changeSpecInfo: function () {
        let checkedNameValue = this.getCheckedSpecValue();
        console.log("checkedNameValue", checkedNameValue);
    },
    //打开规则选择界面
    switchAttrPop: function () {
        // console.log(this);
        if (this.data.openAttr == false) {
            this.setData({
                openAttr: !this.data.openAttr
            })
        }
    },
    // 关闭规则选择界面
    closeAttr: function () {
        this.setData({
            openAttr: false
        })
    },
    //获取商品信息
    getGoodsInfo: function () {
        let that = this;
        util.request(api.GoodsDetail, {
            id: that.data.id
        }).then(function (res) {
            console.log(res);
            if (res.errno === 0) {
                that.setData({
                    goods: res.data.info,
                    attribute: res.data.attribute,
                    issueList: res.data.issue,
                    comment: res.data.comment,
                    brand: res.data.brand,
                    specificationList: res.data.specificationList,
                    productlist: res.data.productlist,
                    checkedSpecPrice: res.data.info.retailPrice,
                    content: res.data.info.detail.replace(/\<img/gi, '<img class="diy-img"'),
                })
                that.getGoodsRelated();
            }
        })
    },
    //获取大家都在看的相关商品
    getGoodsRelated: function () {
        let that = this;
        util.request(api.GoodsRelated, {
            id: that.data.id
        }).then(function (res) {
            if (res.errno === 0) {
                that.setData({
                    relatedGoods: res.data.goodsList,
                })
            }
        })
    },
    //点击-1
    cutNumber:function(){
        this.setData({
            number:(this.data.number - 1 > 1) ? this.data.number - 1 : 1
        })
    },
    //点击+1
    addNumber:function(){
        this.setData({
            number:this.data.number + 1
        })
    },
    onLoad(options) {
        if (options.id) {
            this.setData({
                id: parseInt(options.id)
            })
            this.getGoodsInfo()
        }
    }
})